<?php

namespace app\common\model\order\goods;

use app\common\model\order\Goods;
use app\common\model\User;
use app\common\model\user\Info;
use think\Model;


class Log extends Model
{





    // 表名
    protected $name = 'order_goods_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_settle_text'
    ];



    public function getIsSettleList()
    {
        return ['0' => __('Is_settle 0'), '1' => __('Is_settle 1')];
    }


    public function getIsSettleTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_settle']) ? $data['is_settle'] : '');
        $list = $this->getIsSettleList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function order()
    {
        return $this->belongsTo('app\common\model\order\Order', 'order_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function goods()
    {
        return $this->belongsTo('app\common\model\order\Goods', 'og_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    static public function add_performance_detail($order, $time)
    {
        $ordergoods = Goods::all(['order_id' => $order->id]);
        $userinfo = User::get($order->user_id);
        foreach ($ordergoods as $k => $v) {
            $info = Info::where("user_id", $userinfo['id'])->where('first_cate', $v['first_cate'])->where('two_cate', $v['two_cate'])->find();
            if (!$info) {
                $shareholder = 0;
                $investment = 0;
            } else {
                $shareholder = $info['shareholder'];
                $investment = $info['investment'];
            }
            $uids = Info::whereIn("user_id", $userinfo['paren_ids'])->where('shareholder', '>', $shareholder)->where('first_cate', $v['first_cate'])->where('two_cate', $v['two_cate'])->order('user_id desc')->select();
            if ($uids) {
                $sq = $shareholder;
                foreach ($uids as $vv) {
                    if ($vv['shareholder'] <= $sq) {
                        continue;
                    }
                    Log::create([
                        'order_id' => $order->id,
                        'goods_id' => $v['goods_id'],
                        'og_id' => $v['id'],
                        'goods_num' => $v['goods_num'],
                        'first_cate' => $v['first_cate'],
                        'two_cate' => $v['two_cate'],
                        'u_id' => $order->user_id,
                        'user_id' => $vv['user_id'],
                        'money' => $v['pay_fee'],
                        'p_shareholder' => $vv['shareholder'],
                        'shareholder' => $shareholder,
                        'createtime' => $time,
                    ]);
                    $sq = $vv['shareholder'];
                }
            }
            $uidss = Info::where("investment", 6)->where('first_cate', $v['first_cate'])->where('two_cate', $v['two_cate'])->order('user_id desc')->select();
            if ($uidss) {
                foreach ($uidss as $vv) {
                    Log::create([
                        'order_id' => $order->id,
                        'goods_id' => $v['goods_id'],
                        'og_id' => $v['id'],
                        'goods_num' => $v['goods_num'],
                        'first_cate' => $v['first_cate'],
                        'two_cate' => $v['two_cate'],
                        'u_id' => $v['user_id'],
                        'user_id' => $vv['user_id'],
                        'money' => $v['pay_fee'],
                        'p_investment' => $vv['investment'],
                        'investment' => $investment,
                        'createtime' => $time,
                    ]);
                }
            }
        }
    }
}
